package mastermind.gui.actions;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import mastermind.game.gameLoop;
import mastermind.gui.Frame;
import mastermind.socket.protocol;
import mastermind.socket.socketConnect;

public class StartGameAction extends AbstractAction {

	private Frame frame;
	int response;
	
	public StartGameAction(Frame frame)
	{
		this.frame = frame;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		if(frame.getLoginStatus() == true)
		{
			socketConnect connect = frame.connect();
			DataInputStream dio = connect.getInputStream();
			DataOutputStream dos = connect.getOutputStream();
			
			if(connect != null && dio != null && dos != null)
			{
				Thread thread = new gameLoop(frame);
				thread.start();
				JButton startButton  = frame.getStartGameButton();
				startButton.setEnabled(false);
				frame.setGameStarted(true);	//start game and disable button till game is over			
			}
		}
		else
		{
			String msg = "You must login first";
			JOptionPane.showConfirmDialog((Component) null, msg, "Error", JOptionPane.DEFAULT_OPTION);
		}
	}

}
